package com.ruoyi.system.controller;

import com.ruoyi.system.domain.SysAlarm;
import com.ruoyi.system.domain.SysConfiguration;
import com.ruoyi.system.service.ISysAlarmService;
import com.ruoyi.system.service.ISysConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author rhh
 */
@RestController
public class RsuAjaxController {

    private final ISysAlarmService sysAlarmService;

    private final ISysConfigurationService sysConfigurationService;

    @Autowired
    RsuAjaxController(ISysAlarmService sysAlarmService, ISysConfigurationService sysConfigurationService) {
        this.sysAlarmService = sysAlarmService;
        this.sysConfigurationService = sysConfigurationService;
    }

    @RequestMapping(value = "/system/ajax.execute", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public String execute(HttpServletRequest request) {
        String id = request.getParameter("id");
        SysAlarm alarm = sysAlarmService.selectSysAlarmById(id);
        if ("1".equals(alarm.getAlarmStatus())) {
            return "该告警信息已处理";
        }
        String solutionId = alarm.getAlarmSolution();
        if ("0".equals(solutionId)) {
            //重启
            return restart(alarm.getAlarmDeviceId());
        } else if ("1".equals(solutionId)) {
            //更新配置参数
            return updateConfiguration(alarm.getAlarmDeviceId());
        }

        return "Error";
    }

    private String updateConfiguration(String alarmDeviceId) {
        //更新配置的逻辑
         SysConfiguration configuration = sysConfigurationService.selectSysConfigurationById(alarmDeviceId);
         return "ResetConfig";
    }

    private String restart(String alarmDeviceId) {
        //重启的逻辑
        return "REBOOT";
    }
}
